//
//  AXFMessageMainVC.m
//  Message
//
//  Created by 候锐 on 16/11/20.
//  Copyright © 2016年 候锐. All rights reserved.
//

#import "AXFMessageMainVC.h"
#import "AXFMessageCell.h"
#import "UIColor+CZAddition.h"
#import "Masonry.h"

@interface AXFMessageMainVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak) UITableView *tableView;
@property(nonatomic,weak)UIView *userView;
@property(nonatomic,weak)UIView *systemView;
@end

@implementation AXFMessageMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];

}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    
//    self.navigationController.navigationBar.hidden = YES;
//}


- (void)setupUI{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    NSArray*segmentedArray = @[@"用户消息",@"系统消息"];
    UISegmentedControl*segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor= [UIColor cz_colorWithHex:0xfdd431];
    segmentedControl.frame = CGRectMake(0.0, 0.0, 290, 30.0);
    
    
    //    选中的颜色
    [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    //    未选中的颜色
    [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
    
    
    [self.navigationController.navigationBar.topItem setTitleView:segmentedControl];

    
    [segmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];
    
    //创建两个视图
    UIView *systemview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    systemview.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    self.systemView = systemview;
    [self.view addSubview:systemview];

    
    UIView *userview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    userview.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    self.userView = userview;
    [self.view addSubview:userview];
//    [self ifHaveMessageAtView:self.userView];
    [self ifEmptyMessageAtView:self.userView];
    

}
-(void)selected:(id)sender{

    UISegmentedControl* control = (UISegmentedControl*)sender;
    switch (control.selectedSegmentIndex) {
        case 0:
            self.userView.hidden = NO;
            self.systemView.hidden = YES;
            break;
        case 1:
            self.userView.hidden = YES;
            self.systemView.hidden = NO;
//            [self ifEmptyMessageAtView:self.systemView];
            [self ifHaveMessageAtView:self.systemView];
            break;
    }
}

- (void)ifEmptyMessageAtView:(UIView *)view{
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v2_my_message_empty"]];
    [view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.centerY.equalTo(self.view).offset(-60);
        make.centerX.equalTo(self.view);
        make.height.width.mas_equalTo(85);
    }];
    
    UILabel *messageLabel = [[UILabel alloc] init];
    messageLabel.text = @"还没有消息哦";
    messageLabel.textColor = [UIColor grayColor];
    [view addSubview:messageLabel];
    
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).mas_offset(15);
        make.centerX.equalTo(self.view);
    }];
}
- (void)ifHaveMessageAtView:(UIView *)view{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    self.tableView = tableView;
    
    [view addSubview:tableView];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.estimatedRowHeight = 91;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AXFMessageCell" bundle:nil] forCellReuseIdentifier:@"messagecell"];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(-33);
    }];
    self.tableView.tableFooterView = [[UIView alloc] init];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        AXFMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messagecell" forIndexPath:indexPath];

    return cell;
}


@end
